<template>
  <div class="home">
    <div
      style="
        margin: 10px 0 0 0;
        display: flex;
        justify-content: space-between;
        align-items: center;
      "
    >
      <div>车位列表</div>
      <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>停车场管理</el-breadcrumb-item>
          <el-breadcrumb-item>停车场信息</el-breadcrumb-item>
          <el-breadcrumb-item>车位列表</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
    </div>
    <!-- 图表区 -->
    <div class="highcharts">
      <div style="width: 400px">
        <highcharts
          :options="chartOptions"
          style="width: 400px; height: 200px"
        ></highcharts>
      </div>
      <div style="width: 400px; margin-left: 50px; position: relative">
        <strong style="position: absolute; top: 0; left: 110px"
          >车位统计</strong
        >
        <div ref="main" id="main"></div>
        <div class="highcharts-header">
          <p>总车位数：</p>
          <p>{{this.temporary+this.monthlyCard}}</p>
        </div>
        <div class="highcharts-footer">
          <div
            style="margin-right: 10px"
            v-if="this.temporary || this.monthlyCard"
          >
            <p>
              {{
                (
                  (this.temporary / (this.temporary + this.monthlyCard)) *
                  100
                ).toFixed()
              }}%
            </p>
            <p>
              {{
                (
                  (this.monthlyCard / (this.temporary + this.monthlyCard)) *
                  100
                ).toFixed()
              }}%
            </p>
          </div>
          <div>
            <p>{{ this.temporary }}个</p>
            <p>{{ this.monthlyCard }}个</p>
          </div>
        </div>
      </div>
    </div>

    <!-- 搜索区 重置区-->
    <div class="header">
      <el-input clearable v-model="id" placeholder="车位编号"></el-input>
      <el-input clearable v-model="carNum" placeholder="车牌号码"></el-input>
      <el-select clearable v-model="cwType" placeholder="车位类型">
        <el-option label="临时车位" value="0"></el-option>
        <el-option label="月卡车位" value="1"></el-option>
      </el-select>
      <el-select clearable v-model="cwStatus" placeholder="车位状态">
        <el-option label="无车" value="0"></el-option>
        <el-option label="有车" value="1"></el-option>
      </el-select>
      <el-button
        type="primary"
        class="el-icon-search"
        size="medium"
        @click="search"
        >查询</el-button
      >
      <el-button class="el-icon-refresh" size="medium" @click="reset"
        >重置</el-button
      >
    </div>

    <!-- 数据 -->
    <div style="width: auto; background: #fff; margin-top: 10px">
      <div
        style="
          display: flex;
          justify-content: space-between;
          padding: 10px 0 10px 0;
        "
      >
        <p style="font-size: 15px; font-weight: bold; margin: 5px 0 0 20px">
          查询结果列表
        </p>
        <div style="margin: 0 30px 0 0">
          <el-button
            style="background: #0bb197"
            type="success"
            @click="addDialog = true"
            size="small"
            >新增</el-button
          >
        </div>
      </div>
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="序号" width="100" align="center">
        </el-table-column>
        <el-table-column prop="spaceNum" label="车位编号" align="center">
        </el-table-column>
        <el-table-column prop="parkingName" label="停车场名称" align="center">
        </el-table-column>
        <el-table-column prop="parkingType" label="停车场类型" align="center">
          <template slot-scope="scope">
            <span>{{
              scope.row.parkingType == 0
                ? "地下停车场"
                : scope.row.parkingType == 1
                ? "地面停车场"
                : "地上停车楼"
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="spaceType" label="车位类型" align="center">
          <template slot-scope="scope">
            <span>{{
              scope.row.spaceType == 0 ? "临时车位" : "月卡车位"
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="lockNum" label="地锁编号" align="center">
        </el-table-column>
        <el-table-column prop="bindLicense" label="绑定车牌" align="center">
        </el-table-column>
        <el-table-column prop="carOwner" label="车主名称" align="center">
        </el-table-column>
        <el-table-column prop="spaceState" label="车位状态" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.spaceState == 0 ? "无车" : "有车" }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              style="color: #0bb197"
              @click="edit(scope.row) < (editDialog = true)"
              type="text"
              size="small"
              >车位设置</el-button
            >
            <el-button
              style="color: #0bb197"
              @click="del(scope.row.id)"
              type="text"
              size="small"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage4"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="count"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :background="true"
        class="msg-pagination-container"
      >
      </el-pagination>
    </div>

    <!-- 编辑的弹出框 -->
    <el-dialog title="车位设置" :visible.sync="editDialog">
      <el-form :model="editData">
        <div class="addBox">
          <el-form-item label="车位编号" style="width: 45%; margin-right: 10%">
            <el-input
              disabled
              v-model="editData.spaceNum"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="停车场名称" style="width: 45%">
            <el-input disabled v-model="editData.parkingName" autocomplete="off">
            </el-input>
          </el-form-item>
        </div>

        <div class="addBox" style="justify-content: space-between">
          <el-form-item label="停车场类型" style="width: 45%">
            <el-select disabled
              v-model="editData.parkingType"
              placeholder=""
              style="width: 100%; justify-content: space-between"
            >
              <el-option label="地下停车场" value="0"></el-option>
              <el-option label="地面停车场" value="1"></el-option>
              <el-option label="地上停车楼" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="车位类型" style="width: 45%">
            <el-select
              v-model="editData.spaceType"
              placeholder=""
              style="width: 100%; justify-content: space-between"
            >
              <el-option label="临时车位" value="0"></el-option>
              <el-option label="月卡车位" value="1"></el-option>
            </el-select>
          </el-form-item>
        </div>

        <div class="addBox">
          <el-form-item label="地锁编号" style="width: 45%; margin-right: 10%">
            <el-input disabled v-model="editData.lockNum" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="绑定车牌" style="width: 45%">
            <el-input
              v-model="editData.bindLicense"
              autocomplete="off"
            >
            </el-input>
          </el-form-item>
        </div>

        <div class="addBox">
          <el-form-item label="车主名称" style="width: 45%; margin-right: 10%">
            <el-input v-model="editData.carOwner" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="车位状态" style="width: 45%">
            <el-select
              v-model="editData.spaceState"
              placeholder=""
              style="width: 100%; justify-content: space-between"
            >
              <el-option label="无车" value="0"></el-option>
              <el-option label="有车" value="1"></el-option>
            </el-select>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type=""
          style="margin-right: 30px"
          class="reset"
          @click="editDialog = false"
          >取 消</el-button
        >
        <el-button type="success" class="btnColor" @click="editFn"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <!-- 新增的弹出框 -->
    <el-dialog title="新增区域" :visible.sync="addDialog">
      <el-form :model="addData">
        <div class="addBox">
          <el-form-item label="车位编号" style="width: 45%; margin-right: 10%">
            <el-input v-model="addData.spacesNum" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="关联停车场" style="width: 45%">
            <el-select
              @change="changeFn(addData.parkingNum)"
              clearable
              v-model="addData.parkingNum"
              placeholder=""
              style="width: 100%"
            >
              <el-option
                :key="v.id"
                v-for="v in parkingAll"
                :label="v.parkingName"
                :value="v.parkingNum"
              ></el-option>
            </el-select>
          </el-form-item>
        </div>

        <div class="addBox" style="justify-content: space-between">
          <el-form-item label="停车场类型" style="width: 45%">
            <el-select
              clearable
              v-model="addData.parkingType"
              placeholder=""
              style="width: 100%; justify-content: space-between"
            >
              <el-option label="地下停车场" value="0"></el-option>
              <el-option label="地面停车场" value="1"></el-option>
              <el-option label="地上停车楼" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="区域" style="width: 45%">
            <el-select
              v-model="addData.regionNum"
              placeholder=""
              style="width: 100%; justify-content: space-between"
            >
              <el-option :key="v.regionNum"
                v-for="v in regionData"
                :label="v.regionName"
                :value="v.regionNum"></el-option>
            </el-select>
          </el-form-item>
        </div>

        <div class="addBox">
          <el-form-item label="地锁编号" style="width: 45%; margin-right: 10%">
            <el-input v-model="addData.lockNum" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="绑定车牌" style="width: 45%">
            <el-input v-model="addData.bindLicense" autocomplete="off">
            </el-input>
          </el-form-item>
        </div>

        <div class="addBox">
          <el-form-item label="车主名称" style="width: 45%; margin-right: 10%">
            <el-input v-model="addData.carOwner" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="车位状态" style="width: 45%">
            <el-select
              v-model="addData.spaceState"
              placeholder=""
              style="width: 100%; justify-content: space-between"
            >
              <el-option label="无车" value="0"></el-option>
              <el-option label="有车" value="1"></el-option>
            </el-select>
          </el-form-item>
        </div>

        <el-form-item label="车位类型" style="width: 45%">
            <el-select
              v-model="addData.spaceType"
              placeholder=""
              style="width: 100%; justify-content: space-between"
            >
              <el-option label="临时车位" value="0"></el-option>
              <el-option label="月卡车位" value="1"></el-option>
            </el-select>
          </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type=""
          style="margin-right: 30px"
          class="reset"
          @click="addDialog = false"
          >取 消</el-button
        >
        <el-button type="success" class="btnColor" @click="add"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as echarts from "echarts";
import { Chart } from "highcharts-vue";

export default {
  data: function () {
    return {
      temporary: 0,
      monthlyCard: 0,
      page: 1,
      count: 5,
      total: 1,
      id: "",
      carNum: "",
      cwType: "",
      cwStatus: "",
      addDialog: false,
      editDialog: false,
      currentPage4: 1,
      formLabelWidth: "",
      num: 100,
      tableData: [],
      tableData1: [],
      addData: {
        parkingNum:'',
        spaceNum:'',
	      bindLicense:'' ,
	      carOwner: '',
	      deleted: 0,
        id:0,
	      lockNum: '',
	      regionNum:'' ,
	      spaceState: '',
	      spacesNum: '',
	      spacesType: 0,
        parkingName: "",
        redionName: "",
        regoinNum: "",
      },
      editData: {},
      parkingAll: [],
      regionData:[],
      chartOptions: {
        credits: { enabled: false },
        title: {
          text: "24小时车流量态势",
          align: "left",
        },
        yAxis: {
          title: {
            text: "",
          },
        },
        legend: {
          layout: "vertical",
          align: "right",
          verticalAlign: "middle",
        },
        plotOptions: {
          series: {
            label: {
              connectorAllowed: true,
            },
            pointStart: 0,
            pointInterval: 2,
            showInLegend: false,
          },
        },
        series: [
          {
            name: "",
            data: [190, 150, 100, 80, 220, 280, 240, 260, 220, 200, 40, 80],
          },
        ],
        responsive: {
          rules: [
            {
              condition: {
                maxWidth: 400,
                minHeight: 200,
              },
            },
          ],
        },
      },
    };
  },
  components: {
    highcharts: Chart,
  },
  methods: {
    //改变条数
    handleSizeChange(val) {
      this.count = val;
      this.getData();
      // console.log(`每页 ${val} 条`);
    },
    //翻页
    handleCurrentChange(val) {
      this.page = val;
      this.getData();
      // console.log(`当前页: ${val}`);
    },
    //渲染
    getData: function () {
      this.$axios({
        url: "/smart/parking-spaces/list",
        method: "POST",
        params: {
          pageNum: this.page,
          pageSize: this.count,
        },
        data: {
          spacesNum: this.id,
          bindLicense: this.carNum,
          spaceState: this.cwStatus,
          spaceType: this.cwType,
        },
      }).then(({ data }) => {
        console.log(data);
        if (data.code == "00000") {
          this.tableData = data.data.list;
          this.total = data.data.total;
        } else {
          this.$message.error("数据加载失败！");
        }
      });
    },
    //搜索
    search: function () {
      this.getData();
    },
    //重置
    reset: function () {
      this.id = "";
      this.carNum = "";
      this.cwType = "";
      this.cwStatus = "";
      this.getData();
    },
    //新增
    add: function () {
      console.log('新增数据检测',this.addData);
      this.$axios({
        url: "/smart/parking-spaces/add",
        method: "post",
        data: this.addData,
      }).then(({ data }) => {
        console.log('新增数据测试',data)
        if (data.code == '00000') {
          this.$message.success("添加成功");
          this.addDialog = false;
          this.getData();
          this.getData1();
          this.ChartFn();
        } else {
          this.$message.error("添加失败");
        }
      });
    },
    //车位设置
    edit: function (index) {
      let editData = { ...index };
      editData.parkingState = editData.parkingState + "";
      editData.parkingType = editData.parkingType + "";
      editData.spaceState = editData.spaceState + "";
      this.editData = editData;
      console.log(this.editData);
    },
    editFn: function () {
      this.$axios({
        url: "/smart/parking-spaces/edit",
        method: "PUT",
        data: this.editData,
      }).then(({ data }) => {
        console.log("测试数据", data);
        if (data.code == "00000") {
          this.$message.success("修改成功");
          this.editDialog = false;
          this.getData();
          this.getData1()
          this.ChartFn();
        } else {
          this.$message.error("修改失败");
        }
      });
    },
    //删除
    del: function (index) {
      this.$axios({
        url: "/smart/parking-spaces/del/" + index,
        method: "DELETE",
      }).then(({ data }) => {
        console.log("删除数据检测", data);
        if (data.code == "00000") {
          this.$message.success("删除成功");
          this.getData();
          this.getData1()
          this.ChartFn();
        } else {
          this.$message.error("删除失败");
        }
      });
    },
    //数据改变的时候
    changeFn: function (index) {
      console.log('停车场编号',index)
      console.log("数据变化", this.addData.parkingNum);
      this.parkingAll.forEach((v) => {
        if (v.parkingNum == this.addData.parkingNum) {
          this.addData.parkingType = v.parkingType + "";
          this.addData.parkingName = v.parkingName;
          console.log("类型", this.addData.parkingType);
          console.log("停车场名字", this.addData.parkingName);
        };
      });
      this.$axios({
        url: "/smart/p-region/list",
        method: "GET",
        params: {
          pageNum: 1,
          pageSize: 100000,
          parkingName: this.addData.parkingName,
        },
      }).then(({ data }) => {
        console.log('区域数据获取',data);
        if (data.code == "00000") {
          // this.$message.success("区域数据获取成功！");
          this.regionData = data.data.list;
        } else {
          this.$message.error("区域数据获取失败！");
        }
      });
    },
    //图表
    ChartFn: function () {
      this.temporary = 0;
      this.monthlyCard = 0;
      setTimeout((v) => {
        // console.log("超超超超超超", this.tableData1);
        this.tableData1.forEach((v) => {
          if (v.spaceType == 0) {
            this.temporary++;
          } else if (v.spaceType == 1) {
            this.monthlyCard++;
          }
          console.log("车位", this.temporary, this.monthlyCard);
          var myChart = echarts.init(this.$refs.main);
          let data = {
            // title: {
            //   text: '车位统计',
            //   left: 'left'
            // },
            tooltip: {
              trigger: "item",
            },
            legend: {
              top: "5%",
              left: "right",
              orient: "vertical",
              align: "left",
              padding: 35,
            },
            series: [
              {
                name: "Access From",
                type: "pie",
                radius: ["40%", "70%"],
                avoidLabelOverlap: false,
                // itemStyle: {
                //   borderRadius: 10,
                //   borderColor: "#fff",
                //   borderWidth: 2,
                // },
                label: {
                  show: false,
                  position: "center",
                },
                emphasis: {
                  label: {
                    // show: true,
                    fontSize: "40",
                    fontWeight: "bold",
                  },
                },
                labelLine: {
                  show: false,
                },
                data: [
                  { value: this.temporary, name: "临时车位" },
                  { value: this.monthlyCard, name: "月卡车位" },
                ],
              },
            ],
          };
          myChart.setOption(data);
        });
      }, 500);
    },

    //获取所有车位
    getData1:function(){
    this.$axios({
        url: "/smart/parking-spaces/list",
        method: "POST",
        params: {
          pageNum: 1,
          pageSize: 10000,
        },
        data: {
          spacesNum: '',
          bindLicense: '',
          spaceState: '',
          spaceType: '',
        },
      }).then(({ data }) => {
        console.log(data);
        if (data.code == "00000") {
          this.tableData1 = data.data.list;
          this.total = data.data.total;
        } else {
          this.$message.error("数据加载失败！");
        }
      });
    }
  },
  mounted: function () {
    //初始化渲染
    this.getData();
    this.ChartFn();
    this.getData1();

    //获取所有停车场
    this.$axios({
      url: "/smart/parking/list",
      method: "POST",
      params: {
        pageNum: 1,
        pageSize: 10000,
      },
      data: {
        parkingNum: "",
        parkingName: "",
        parkingType: "",
      },
    }).then(({ data }) => {
      console.log("所有停车场", data);
      if (data.code == "00000") {
        this.parkingAll = data.data.list;
        console.log("挂载后测试", this.parkingAll);
      } else {
        this.$message.error("停车场加载失败！");
      }
    });

  },
};
</script>

<style lang="less" scoped>
.home {
  height: 95%;
  margin: 20px 20px 0 20px;
}
.header {
  width: 100%;
  height: 80px;
  background: #fff;
  display: flex;
  align-items: center;

  .el-input,
  .el-select {
    width: 200px;
    margin: 10px;
  }
}
.el-icon-search {
  background: #0bb197;
  margin-left: 10px;
}
.el-icon-refresh {
  background-color: white;
  color: #c0c4cc;
  border: 1px solid #c0c4cc;
}
.el-icon-refresh:hover {
  color: #0bb197;
}
.el-pagination {
  margin-top: 30px;
  text-align: right;
}

.highcharts {
  display: flex;
  width: 100%;
  height: 210px;
  background: #fff;
  margin-bottom: 20px;
}
#main {
  max-width: 400px;
  height: 200px;
  width: 400px;
  position: absolute;
  top: 10px;
  left: 0;
}
.highcharts-header {
  position: absolute;
  top: 90px;
  left: 165px;
  font-size: 15px;

  p {
    text-align: center;
  }
}
.highcharts-footer {
  width: auto;
  display: flex;
  font-size: 13px;
  position: absolute;
  top: 53px;
  right: -30px;
}
.highcharts-footer p {
  padding-bottom: 7px;
}
//编辑弹框样式
.el-dialog {
  width: 35%;
}
.btnColor {
  background-color: #0bb197;
}
.dialog-footer {
  display: flex;
  justify-content: center;
}
.addBox {
  width: 100%;
  display: flex;
  justify-content: space-evenly;
}
.reset {
  background-color: white;
  color: #c0c4cc;
  border: 1px solid #c0c4cc;
}
.reset:hover {
  color: #0bb197;
}
.regionSet {
  .el-dialog {
    width: 820px;
  }
}
</style>
<style>
.msg-pagination-container.is-background .el-pager li:not(.disabled).active {
  /*当前选中页数的样式进行修改*/
  background-color: #0bb197;
  color: #fff;
}
</style>
